from selenium.webdriver.common.by import By
import time
from base_page import BasePage


class HeroListPageLocators:
    # Locators for the hero list page
    WEEKLY_FREE_RADIO = (By.CSS_SELECTOR, "span[class='ms-radio']:first-child")
    NEWBIE_RADIO = (By.CSS_SELECTOR, "li[data-ptype='11'] > span")
    HERO_LIST_ITEMS = (By.CSS_SELECTOR, ".herolist > li")
    SUMMONER_TAB = (By.CSS_SELECTOR, ".herolist-nav > li:nth-child(3) >a")
    SUMMONER_LIST_ITEMS = (By.CSS_SELECTOR, "#spellList > li >p")


class HeroListPage(BasePage):
    URL = "https://pvp.qq.com/web201605/herolist.shtml"

    def open(self):
        self.visit(self.URL)

    def select_weekly_free_filter(self):
        self.click(HeroListPageLocators.WEEKLY_FREE_RADIO)

    def select_newbie_filter(self):
        self.click(HeroListPageLocators.NEWBIE_RADIO)

    def get_displayed_hero_elements(self):
        return self._find_elements(HeroListPageLocators.HERO_LIST_ITEMS)

    def get_displayed_hero_names(self):
        hero_elements = self._find_elements(HeroListPageLocators.HERO_LIST_ITEMS)
        hero_names = [h.text.strip() for h in hero_elements]
        return hero_names

    def get_newbie_hero_names(self):
        hero_elements = self._find_elements(HeroListPageLocators.HERO_LIST_ITEMS)
        hero_names = [h.text.strip() for h in hero_elements]
        return hero_names

    def select_summoner_filter(self):
        self.maximum()
        self.click(HeroListPageLocators.SUMMONER_TAB)
        time.sleep(5)
        self.switch_tab(self.driver.window_handles[1])

    def get_summoner_spells(self):
        summoner_elements = self._find_elements(
            HeroListPageLocators.SUMMONER_LIST_ITEMS
        )
        for h in summoner_elements:
            print(h.text)
        spell_names = [h.text for h in summoner_elements]
        return spell_names
